package com.soft.base.enums;

import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
/**
 * class
 *
 * @author user
 * @date 2020/06/05
 */
public class ValueNameIntegerListDeserializer implements ObjectDeserializer {
    @Override
    public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
        List<Integer> bytes = parser.parseArray(Integer.class);
        T list = (T) new ArrayList<>();
        Object[] enumConstants = ((Class) ((ParameterizedTypeImpl) type).getActualTypeArguments()[0]).getEnumConstants();
        for (Integer aInteger : bytes) {
            for (Object o : enumConstants) {
                if (((ValueNameEnum) o).getValue().equals(aInteger)) {
                    ((List) list).add(o);
                }
            }
        }
        return list;
    }

    @Override
    public int getFastMatchToken() {
        return 0;
    }
}
